﻿using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using DongFangGuoKai.WMSPlatform.Service.BasicApp.Pallet;
using DongFangGuoKai.WMSPlatform.Service.BasicApp.Pallet.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.BasicData
{
    /// <summary>
    /// 托盘管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PalletController : ApiBaseController
    {
        private readonly IPalletService palletService;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="palletService"></param>
        public PalletController(IPalletService palletService)
        {
            this.palletService = palletService;
        }

        /// <summary>
        /// 批量添加托盘
        /// </summary>
        /// <param name="addListPalletDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BatchPalletForm(AddListPalletDTO addListPalletDTO)
        {
            var IsOk = palletService.AddPalletList(addListPalletDTO,out string errMsg);
            if (IsOk)
            {
                return Success(errMsg);
            }
            return Failed(errMsg);
        }

        /// <summary>
        /// 查询托盘数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPallets(string? name, int type = -1, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed("分页条件有误！");
            }
            var list = palletService.GetPallets(name, type, pageIndex, pageSize, out int totalCount);

            if (list != null)
            {
                return Success(list,totalCount,"查询成功！");
                
            }
            return Failed("查询失败！");
        }

        /// <summary>
        /// 箱子或托盘绑定储位
        /// </summary>
        /// <param name="bindPalletCellDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindPalletCell(BindPalletCellDTO bindPalletCellDTO)
        {
            var IsOk = palletService.BindPalletCell(bindPalletCellDTO,out string errMsg);
            if (IsOk)
            {
                return Success(errMsg);
            }
            return Failed(errMsg);
        }

        /// <summary>
        /// 箱子或托盘绑定储位
        /// </summary>
        /// <param name="bindPalletCellDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindPalletCell2(BindPalletCellDTO bindPalletCellDTO)
        {
            var IsOk = palletService.BindPalletCell2(bindPalletCellDTO, out string errMsg);
            if (IsOk)
            {
                return Success(errMsg);
            }
            return Failed(errMsg);
        }

        /// <summary>
        /// 托盘打印
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PallPrintCount(string[] codes)
        {
            var IsOk = palletService.PallPrintCount(codes, out string errMsg);
            if (IsOk)
            {
                return Success(errMsg);
            }
            return Failed(errMsg);
        }
    }
}
